//
//  ConActiveValueController.m
//  Shengzhuli
//
//  Created by FXGuo on 2017/9/6.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "ConActiveValueController.h"
#import "ActivityHeaderCell.h"
#import "ActivityValueDetailCell.h"
#import "ConExchangeViewController.h"
#import "ActivityNetWork.h"
#import "SZLAccountHelper.h"
#import "MBProgressHUD+MJ.h"
#import "IntroduceViewController.h"
#import "RecordModel.h"
#import "GGIndicatorHud.h"
#import <MJRefresh.h>

@interface ConActiveValueController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) NSMutableArray *recordDataArray;

@property (nonatomic,assign) int PageNo;
@end

@implementation ConActiveValueController

- (void)viewDidLoad {
    [super viewDidLoad];
    _PageNo = 1;
    self.dataArray = [[NSMutableArray alloc] init];
    self.recordDataArray = [NSMutableArray array];
    [self loadData];
    [self loadRecordData];
    self.title = @"活跃值";
    [self setUpNavi];
    [self.view addSubview:self.tableView];
    
}
- (void)loadRecordData {
    NSLog(@"hhahahhah");
    [GGIndicatorHud showIndicatorInView:self.view];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"token"] = [SZLAccountHelper shareInstance].token;
    param[@"pageNo"] = @(_PageNo);
    [ActivityNetWork getActivityRecordyWithParame:param response:^(BOOL success, NSString *msg, NSArray *recordArr) {
        [GGIndicatorHud hideIndicatorHudInView:self.view];
        if (success) {
            NSLog(@"----%lu----",(unsigned long)recordArr.count);
            [_tableView.mj_header endRefreshing];
            [_tableView.mj_footer endRefreshing];
            if (recordArr.count > 0) {
                _PageNo++;
            } else {
                
                [_tableView.mj_footer endRefreshingWithNoMoreData];
            }
            for (NSMutableDictionary *dict in recordArr) {
                RecordModel *model = [[RecordModel alloc] init];
                model.activeContent = dict[@"activeContent"];
                model.activeValue = dict[@"activeValue"];
                model.createDate = dict[@"createDate"];
                
                [_recordDataArray addObject:model];
            }
        }else {
            NSLog(@"xixiixixixxi");
            [MBProgressHUD showError:msg toView:self.view];
        }
        [_tableView reloadData];
        
    }];
}
- (void)loadData {
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"token"] = [SZLAccountHelper shareInstance].token;
    [ActivityNetWork getActivityAccountWithParame:param response:^(BOOL success, NSString *msg,NSDictionary *dict) {
        if (success) {
            [self.tableView.mj_header endRefreshing];
            [self.dataArray addObject:dict];
            
        }else {
            [MBProgressHUD showError:msg];
        }
        [_tableView reloadData];
    }];
    
}
- (void)setUpNavi
{
    UIButton *rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, 30)];
    [rightBtn setTitle:@"兑换商品" forState:UIControlStateNormal];
    [rightBtn setTitleColor:RGBColor(255,255,255) forState:UIControlStateNormal];
    rightBtn.titleLabel.font = Font(16);
    [rightBtn addTarget:self action:@selector(rightClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
}
- (void)rightClick
{
    ConExchangeViewController *vc = [[ConExchangeViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 246;
    }
    return 65;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1+_recordDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = [self.dataArray lastObject];
    if (indexPath.row == 0) {
        ActivityHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ActivityHeaderCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.ableToUseLabel.text = [@"可用" stringByAppendingFormat:@"%@",dict[@"activeValue"]];
        
        cell.unableToUseLabel.text = [@"已用" stringByAppendingFormat:@"%@",[dict objectForKey:@"freezingActiveValue"]];
        cell.introduceButtonBlock = ^{
            IntroduceViewController *ivc = [[IntroduceViewController alloc] init];
            [self.navigationController pushViewController:ivc animated:YES];
        };
        return cell;
    }else {
        
        RecordModel *model = _recordDataArray[indexPath.row-1];
        ActivityValueDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ActivityValueDetailCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.activeContentLabel.text = model.activeContent;
        cell.activeValueLabel.text = model.activeValue;
        
        
        NSTimeInterval _interval=[model.createDate doubleValue] / 1000.0;
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
        NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
        [objDateformat setDateFormat:@"yyyy-MM-dd HH:mm"];
        
        cell.createDateLabel.text = [objDateformat stringFromDate:date];
        return cell;
        
    }
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW,ScreenH-64)];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = RGBColor(239, 239, 244);
        _tableView.tableFooterView = [[UIView alloc]init];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ActivityHeaderCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([ActivityHeaderCell class])];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ActivityValueDetailCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([ActivityValueDetailCell class])];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            _PageNo = 1;
            [_recordDataArray removeAllObjects];
            
            [self loadRecordData];
            [self loadData];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{

            [self loadRecordData];
        }];
    }
    return _tableView;
}

@end
